//
//  CYCouponCell.m
//  cyjf
//
//  Created by tauCross on 2017/9/28.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYCouponCell.h"

#import "CYDottedLine.h"


@implementation CYCouponCellObject

@end



@interface CYCouponCell ()

@property(nonatomic, strong)UIView *bgView;
@property(nonatomic, strong)UIImageView *backgroundImageView;
@property(nonatomic, strong)UIImageView *tagImageView;
@property(nonatomic, strong)UILabel *descLabel;
@property(nonatomic, strong)UILabel *unitLabel;
@property(nonatomic, strong)UILabel *amountLabel;
@property(nonatomic, strong)UILabel *dueLabel;
@property(nonatomic, strong)UILabel *sourceLabel;
@property(nonatomic, strong)UILabel *dateLabel;
@property(nonatomic, strong)UIButton *useButton;
@property(nonatomic, strong)UIImageView *disableImageView;

@property(nonatomic, strong)CYDottedLine *line;
@property(nonatomic, strong)UILabel *expandDescLabel;
@property(nonatomic, strong)UIImageView *expandIcon;
@property(nonatomic, strong)UIButton *expandButton;

@property(nonatomic, strong)UIButton *expandView;
@property(nonatomic, strong)UIImageView *expandBgView;
@property(nonatomic, strong)UILabel *expandLabel;

@property(nonatomic, strong)CYCouponCellObject *object;

@end


@implementation CYCouponCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        
        
        self.expandView = [[UIButton alloc] init];
        [self.contentView addSubview:self.expandView];
        {
            self.expandBgView = [[UIImageView alloc] initWithImage:CYImage(@"coupon_shadow")];
            [self.expandView addSubview:self.expandBgView];
            
            self.expandLabel = [[UILabel alloc] init];
            self.expandLabel.font = [UIFont systemFontOfSize:10];
            self.expandLabel.numberOfLines = 0;
            [self.expandView addSubview:self.expandLabel];
        }

        self.bgView = [[UIView alloc] init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 10;
        [self.contentView addSubview:self.bgView];
        
        self.backgroundImageView = [[UIImageView alloc] init];
        [self.bgView addSubview:self.backgroundImageView];

        self.tagImageView = [[UIImageView alloc] initWithImage:CYImage(@"coupon_tag_deadline")];
        [self.contentView addSubview:self.tagImageView];
        
        self.descLabel = [[UILabel alloc] init];
        self.descLabel.textColor = HEXCOLOR(@"ffffff");
        self.descLabel.font = [UIFont systemFontOfSize:30 weight:UIFontWeightBold];
        [self.contentView addSubview:self.descLabel];
        
        self.unitLabel = [[UILabel alloc] init];
        self.unitLabel.textColor = HEXCOLOR(@"ffffff");
        self.unitLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold];
        [self.contentView addSubview:self.unitLabel];
        
        self.amountLabel = [[UILabel alloc] init];
        self.amountLabel.textColor = HEXCOLOR(@"ffffff");
        self.amountLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.amountLabel];
        
        self.dueLabel = [[UILabel alloc] init];
        self.dueLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.dueLabel];
        
        self.sourceLabel = [[UILabel alloc] init];
        self.sourceLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.sourceLabel];
        
        self.dateLabel = [[UILabel alloc] init];
        self.dateLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:self.dateLabel];
        
        self.useButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 54, 16)];
        self.useButton.layer.masksToBounds = YES;
        self.useButton.layer.cornerRadius = 8;
        self.useButton.layer.borderWidth = 1 / SCREEN_SCALE;
        self.useButton.layer.borderColor = APP_DEFAULT_COLOR_RED.CGColor;
        [self.useButton setTitle:@"立即使用" forState:UIControlStateNormal];
        [self.useButton setTitleColor:APP_DEFAULT_COLOR_RED forState:UIControlStateNormal];
        self.useButton.titleLabel.font = [UIFont systemFontOfSize:10];
        [self.useButton addTarget:self action:@selector(onUseButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.useButton];
        
        self.disableImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
        [self.contentView addSubview:self.disableImageView];
        
        self.line = [[CYDottedLine alloc] init];
        self.line.backgroundColor = [UIColor clearColor];
        self.line.lineWidth = 1 / SCREEN_SCALE;
        self.line.lineColor = HEXCOLOR(@"E9E9E9");
        self.line.lengths = @[@1, @1];
        self.line.phase = 0;
        [self.contentView addSubview:self.line];
        
        self.expandDescLabel = [[UILabel alloc] init];
        self.expandDescLabel.text = @"点击查看适用标的";
        self.expandDescLabel.font = [UIFont systemFontOfSize:10];
        [self.expandDescLabel sizeToFit];
        [self.contentView addSubview:self.expandDescLabel];
        
        self.expandIcon = [[UIImageView alloc] init];
        [self.contentView addSubview:self.expandIcon];
        
        self.expandButton = [[UIButton alloc] init];
        [self.expandButton addTarget:self action:@selector(onExpandButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.expandButton];
    }
    return self;
}

+ (CGFloat)heightForObject:(CYCouponCellObject *)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    CGFloat expandHeight = 0;
    if(object.expand)
    {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:10];
        label.numberOfLines = 0;
        label.text = [NSString stringWithFormat:@"适用标的：%@", object.model.canBuyProject];
        expandHeight = [label sizeThatFits:CGSizeMake(tableView.width - 80, MAXFLOAT)].height + 30;
    }
    return 125 + expandHeight;
}

- (BOOL)shouldUpdateCellWithObject:(CYCouponCellObject *)object
{
    self.object = object;
    
    switch(object.model.status.integerValue)
    {
        case 1:
        {
            self.backgroundImageView.image = CYImage(@"coupon_bg");
            self.dueLabel.textColor = HEXCOLOR(@"666666");
            self.sourceLabel.textColor = HEXCOLOR(@"999999");
            self.dateLabel.textColor = HEXCOLOR(@"999999");
            self.expandDescLabel.textColor = HEXCOLOR(@"999999");
            self.expandLabel.textColor = HEXCOLOR(@"999999");
            self.tagImageView.hidden = object.model.expiring.integerValue != 3;
            self.useButton.hidden = NO;
            self.disableImageView.hidden = YES;
            break;
        }
        case 2:
        {
            self.backgroundImageView.image = CYImage(@"coupon_bg_disable");
            self.dueLabel.textColor = HEXCOLOR(@"cccccc");
            self.sourceLabel.textColor = HEXCOLOR(@"cccccc");
            self.dateLabel.textColor = HEXCOLOR(@"cccccc");
            self.expandDescLabel.textColor = HEXCOLOR(@"cccccc");
            self.expandLabel.textColor = HEXCOLOR(@"cccccc");
            self.tagImageView.hidden = YES;
            self.useButton.hidden = YES;
            self.disableImageView.hidden = NO;
            self.disableImageView.image = CYImage(@"coupon_used");
            break;
        }
        case 3:
        {
            self.backgroundImageView.image = CYImage(@"coupon_bg_disable");
            self.dueLabel.textColor = HEXCOLOR(@"cccccc");
            self.sourceLabel.textColor = HEXCOLOR(@"cccccc");
            self.dateLabel.textColor = HEXCOLOR(@"cccccc");
            self.expandDescLabel.textColor = HEXCOLOR(@"cccccc");
            self.expandLabel.textColor = HEXCOLOR(@"cccccc");
            self.tagImageView.hidden = YES;
            self.useButton.hidden = YES;
            self.disableImageView.hidden = NO;
            self.disableImageView.image = CYImage(@"coupon_disabled");
            break;
        }
        default:
            break;
    }
    
    self.descLabel.text = SAFE_STRING(object.model.vcFinalAmount);
    self.unitLabel.text = SAFE_STRING(object.model.couponUnit);
    self.amountLabel.text = [NSString stringWithFormat:@"满%@元可用", SAFE_NUMBER(object.model.damountLowerLimit).stringValue];
    self.dueLabel.text = [NSString stringWithFormat:@"产品期限: ≥%@天", SAFE_NUMBER(object.model.idayLowerLimit).stringValue];
    self.sourceLabel.text = [NSString stringWithFormat:@"来源: %@", SAFE_STRING(object.model.source)];
    self.dateLabel.text = [NSString stringWithFormat:@"有效期至: %@", [[SAFE_NUMBER(object.model.dtExpireTime) cy_date] cy_stringWithFormat:@"yyyy-MM-dd"]];
    
    self.expandIcon.image = object.expand ? CYImage(@"ico_switch_arrow_top") : CYImage(@"ico_switch_arrow_bottom");
    self.expandLabel.text = [NSString stringWithFormat:@"适用标的：%@", SAFE_STRING(object.model.canBuyProject)];
    self.expandView.hidden = !object.expand;
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.bgView.width = self.contentView.width - 40;
    self.bgView.height = 115;
    self.bgView.centerX = self.contentView.width / 2;
    self.bgView.top = 10;

    [self.backgroundImageView sizeToFit];
    self.backgroundImageView.left = 0;
    self.backgroundImageView.top = 0;

    [self.descLabel sizeToFit];
    self.descLabel.centerY = self.bgView.top + 48;
    [self.unitLabel sizeToFit];
    self.unitLabel.centerY = self.bgView.top + 48;
    CGFloat width = self.descLabel.width + self.unitLabel.width;
    CGFloat left = 82.5 - width / 2;
    self.descLabel.left = left;
    self.unitLabel.left = self.descLabel.right;

    [self.amountLabel sizeToFit];
    self.amountLabel.centerX = 82.5;
    self.amountLabel.centerY = self.bgView.top + 71.5;

    [self.dueLabel sizeToFit];
    self.dueLabel.left = 160;
    self.dueLabel.centerY = self.bgView.top + 24;

    [self.sourceLabel sizeToFit];
    self.sourceLabel.left = 160;
    self.sourceLabel.centerY = 58 + self.bgView.top;
    
    [self.dateLabel sizeToFit];
    self.dateLabel.left = 160;
    self.dateLabel.centerY = self.bgView.top + 73;

    self.tagImageView.left = self.bgView.left;
    self.tagImageView.top = self.bgView.top;

    self.line.height = 5;
    self.line.width = self.contentView.width - 190;
    self.line.left = 160;
    self.line.top = self.bgView.top + 84;
    self.line.startPoint = CGPointMake(0, self.line.bounds.size.height / 2);
    self.line.endPoint = CGPointMake(self.line.bounds.size.width, self.line.bounds.size.height / 2);
    [self.line setNeedsDisplay];
    
    self.expandDescLabel.left = 160;
    self.expandDescLabel.centerY = self.bgView.top + 100;
    
    [self.expandIcon sizeToFit];
    self.expandIcon.right = self.contentView.width - 30;
    self.expandIcon.top = self.bgView.top + 93;
    
    self.expandButton.width = self.line.width;
    self.expandButton.height = 29;
    self.expandButton.left = self.line.left;
    self.expandButton.top = self.line.bottom;
    
    self.expandView.width = self.bgView.width;
    self.expandView.height = self.contentView.height - self.bgView.bottom + 5;
    self.expandView.left = self.bgView.left;
    self.expandView.top = self.bgView.bottom - 10;
    {
        self.expandBgView.frame = self.expandView.bounds;
        
        self.expandLabel.size = [self.expandLabel sizeThatFits:CGSizeMake(self.expandView.width - 40, MAXFLOAT)];
        self.expandLabel.left = 20;
        self.expandLabel.bottom = self.expandView.height - 15;
    }

    self.useButton.right = self.contentView.width - 30;
    self.useButton.top = 15 + self.bgView.top;
    
    self.disableImageView.right = self.contentView.width - 30;
    self.disableImageView.top = self.bgView.top + 10;
}

#pragma mark - Control Events
- (void)onUseButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0070];
    if(self.object.useAction)
    {
        self.object.useAction();
    }
}

- (void)onExpandButtonTouchUpInside
{
    self.object.expand = !self.object.expand;
    if(self.object.expandAction)
    {
        self.object.expandAction();
    }
}

@end
